package com.crr.demo.dao.impl;

import com.crr.demo.dao.EmpDao;
import com.crr.demo.entity.Emp;
import com.crr.demo.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class EmpDaoImpl implements EmpDao {
    Connection conn = DBUtils.getConn();
    PreparedStatement ps = null;
    ResultSet rs = null;
    @Override
    public List selectAll() {

        List<Emp> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement("select * from employee");
            rs=ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("e_name");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                double salary=rs.getInt("salary");
                Emp e = new Emp(id,name,sex,age,salary);
                list.add(e);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.closeAll(rs,ps,conn);
        }
        return list;
    }
}
